package cn.fl.ref.fncprjprdpaysch.service.impl;

import java.util.Date;
import java.util.List;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.api.IPrjQuatPrpsApiService;
import cn.fl.project.condition.PrjPrdPaySchMQueryCondition;
import cn.fl.project.dto.PrjPrdPaySchDDTO;
import cn.fl.project.dto.PrjPrdPaySchMDTO;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.transform.PrjPrdPaySchMVOTransform;
import cn.fl.project.vo.PrjPrdPaySchMVO;
import com.alibaba.fastjson.JSONObject;
import fl.ref.fncprjprdpaysch.dto.FncPrjPrdPaySchDDTO;
import fl.ref.fncprjprdpaysch.dto.FncPrjPrdPaySchMDTO;
import fl.ref.fncprjprdpaysch.service.IFncPrjPrdPaySchMApiService;
import org.apache.ibatis.annotations.Case;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.fl.ref.fncprjprdpaysch.service.IFncPrjPrdPaySchDService;
import cn.fl.ref.fncprjprdpaysch.transform.FncPrjPrdPaySchDVOTransform;
import cn.fl.ref.fncprjprdpaysch.vo.FncPrjPrdPaySchDVO;
import fl.ref.fncprjprdpaysch.condition.FncPrjPrdPaySchDQueryCondition;
import fl.ref.fncprjprdpaysch.service.IFncPrjPrdPaySchDApiService;

@Service
public class FncPrjPrdPaySchDServiceImpl implements IFncPrjPrdPaySchDService {
	private static final Logger logger = LoggerFactory.getLogger(FncPrjPrdPaySchDServiceImpl.class);

	@Autowired
	private IFncPrjPrdPaySchDApiService fncPrjPrdPaySchDApiService;
	@Autowired
	private IFncPrjPrdPaySchMApiService fncPrjPrdPaySchMApiService;
	@Autowired
	private IPrjQuatPrpsApiService prjQuatPrpsApiService;
	@Autowired
	private IPrjPrdPaySchMService prjPrdPaySchMService;

	@Override
	public Long add(FncPrjPrdPaySchDVO entity) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchDApiService.add(FncPrjPrdPaySchDVOTransform.toDTO(entity));
	}

	@Override
	public boolean update(FncPrjPrdPaySchDVO entity) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchDApiService.update(FncPrjPrdPaySchDVOTransform.toDTO(entity));
	}

	@Override
	public FncPrjPrdPaySchDVO selectById(Long id) throws BaseException {
		// TODO Auto-generated method stub
		return FncPrjPrdPaySchDVOTransform.toVO(fncPrjPrdPaySchDApiService.selectById(id));
	}

	@Override
	public List<FncPrjPrdPaySchDVO> searchList(FncPrjPrdPaySchDQueryCondition condition) throws BaseException {
		// TODO Auto-generated method stub
		return FncPrjPrdPaySchDVOTransform.toVOList(fncPrjPrdPaySchDApiService.searchList(condition));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchDApiService.removeById(id);
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchDApiService.searchListPage(page);
	}

	@Override
	public Pager searchListPage(Pager page, String sysCd, String orgCd) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchDApiService.searchListPage(page, sysCd, orgCd);
	}

	@Override
	public void calculateRentForWs (List<PrjPrdPaySchMVO> prjPrdPaySchMList) throws Exception {
		//去重判断
		if (CollectionUtils.isNotEmpty(prjPrdPaySchMList)) {
			FncPrjPrdPaySchDQueryCondition paySchDQueryCondition = new FncPrjPrdPaySchDQueryCondition();
			PrjPrdPaySchMQueryCondition paySchMQueryCondition = new PrjPrdPaySchMQueryCondition();
			for (PrjPrdPaySchMVO prjPrdPaySchMVO : prjPrdPaySchMList) {
				prjPrdPaySchMVO.setCstMgrNm(CurrentThreadContext.getCurrentUserName());
				prjPrdPaySchMVO.setCrtTm(new Date());

				paySchMQueryCondition.setPaySchNo(prjPrdPaySchMVO.getPaySchNo());
				List<PrjPrdPaySchMVO> paySchMVOList = prjPrdPaySchMService.searchList(paySchMQueryCondition);
				if (CollectionUtils.isEmpty(paySchMVOList)) {
					prjPrdPaySchMVO.setPayStsCdNm("系统中未找到该数据，请核验");
					continue;
				}
				if (CollectionUtils.isNotEmpty(paySchMVOList)) {
					paySchDQueryCondition.setPaySchId(paySchMVOList.get(0).getId());
					paySchDQueryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
					paySchDQueryCondition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
					List<FncPrjPrdPaySchDDTO> fncPrjPrdPaySchDDTOList = fncPrjPrdPaySchDApiService.searchList(paySchDQueryCondition);
					BaseLogUtils.info(logger, "calculateRentForWs", "查询保理支付表数据:" + JSONObject.toJSONString(fncPrjPrdPaySchDDTOList), CurrentThreadContext.getCurrentUserName());
					if (CollectionUtils.isNotEmpty(fncPrjPrdPaySchDDTOList)) {
						prjPrdPaySchMVO.setPayStsCdNm("该数据已上传，请核验");
						continue;
					}
				}

				try {
					PrjPrdPaySchMDTO prjPrdPaySchMDTO = prjQuatPrpsApiService.calculateRentForWs(PrjPrdPaySchMVOTransform.toDTO(prjPrdPaySchMVO));
					BaseLogUtils.info(logger, "calculateRentForWs", "保理支付表数据 DATA:" + JSONObject.toJSONString(prjPrdPaySchMDTO), CurrentThreadContext.getCurrentUserName());
					//保存报理支付表
					if (null != prjPrdPaySchMDTO) {
						FncPrjPrdPaySchMDTO fncPrjPrdPaySchMDTO = new FncPrjPrdPaySchMDTO();
						BeanUtils.copyProperties(prjPrdPaySchMDTO, fncPrjPrdPaySchMDTO);
						fncPrjPrdPaySchMApiService.add(fncPrjPrdPaySchMDTO);

						List<PrjPrdPaySchDDTO> prjPrdPaySchDList = prjPrdPaySchMDTO.getPrjPrdPaySchDList();
						if (CollectionUtils.isNotEmpty(prjPrdPaySchDList)) {
							for (PrjPrdPaySchDDTO prjPrdPaySchDDTO : prjPrdPaySchDList) {
								FncPrjPrdPaySchDDTO fncPrjPrdPaySchDDTO = new FncPrjPrdPaySchDDTO();
								BeanUtils.copyProperties(prjPrdPaySchDDTO, fncPrjPrdPaySchDDTO);
								fncPrjPrdPaySchDApiService.add(fncPrjPrdPaySchDDTO);
							}
						}
						prjPrdPaySchMVO.setPayStsCdNm("导入成功");
					}
				}catch (Exception ex) {
					ex.printStackTrace();
					prjPrdPaySchMVO.setPayStsCdNm(ex.getMessage());
				}
			}
		}
	}
}
